package cn.rxmind.dlog.common.sender.db;

import cn.hutool.db.sql.SqlBuilder;
import cn.rxmind.dlog.common.DLogData;
import com.google.common.base.Joiner;

import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 *
 * @description 
 *
 * @author renxin
 * @email renxinzhiliangzhi@163.com
 * @date 2020/7/13 11:29 上午
 */
public abstract class DLogSqlProvider {

    public abstract String getTableName();

    public abstract Set<String> getColumnNames();

    /**
     * 此方法用于拼接批量插入语句
     *
     * @param dLogDataList
     * @return
     */
    public String getInsertSQL(List<DLogData> dLogDataList){

        return new StringBuilder().append("INSERT INTO ")
                .append(getTableName())
                .append("(").append(Joiner.on(',').join(getColumnNames())).append(")")
                .append("VALUES ")
                .append(Joiner.on(',').join(
                        dLogDataList.stream().map(dLogData -> new StringBuilder().append("(")
                                        .append(Joiner.on(',').join(
                                                getColumnNames().stream().map(column -> "?").collect(Collectors.toList()))
                                        ).append(")").toString()
                                ).collect(Collectors.toList())

                )).toString();
    }

    protected abstract Object mappingValue(String column,DLogData dLogData);

}
